﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using Newtonsoft.Json;
using System.Text;
using DotNet.Common.TransferParam;

namespace ZLCMS.Web.SystemManage.Basics.Param.Group
{
    public partial class DeleteExp : System.Web.UI.Page
    {
        string _code;

        FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new FrameHelper(this);
            
            int expListIndex;
            string groupId;
            SysParam gourpDB;
            List<ParamGroupExp> expList;

            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                    groupId = Request["groupid"] == null ? "" : Request["groupid"];
                    _code = Request["code"] == null ? "" : Request["code"];

                    gourpDB = session.CreateCriteria<SysParam>().Add(Expression.Eq("GroupCode", "TranferParam")).Add(Expression.Eq("ItemCode", groupId)).UniqueResult<SysParam>();

                    if (gourpDB == null)
                    {
                        throw new Exception("找不到对应信息.");
                    }

                    expList = JsonConvert.DeserializeObject<List<ParamGroupExp>>(gourpDB.Param5);

                    if (expList == null)
                    {
                        throw new Exception("找不到对应参数扩展信息.");
                    }

                    expListIndex = expList.FindIndex(FindCode);

                    if (expListIndex == -1)
                    {
                        throw new Exception("找不到对应参数扩展项信息.");
                    }

                    expList.RemoveAt(expListIndex);

                    gourpDB.Param5 = JsonConvert.SerializeObject(expList);

                    session.Update(gourpDB);
                    session.Flush();

                }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }

            // 刷新 editparamgroup 页面
            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                navTabId = "editparamgroup"
            });
        }

        protected bool FindCode(ParamGroupExp item)
        {
            if (item.Code == _code)
            {
                return true;
            }
            return false;
        }
    }
}
